package com.test.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.test.entity.ExamModel;

@RequestMapping("/exammodels")
@Controller
@RooWebScaffold(path = "exammodels", formBackingObject = ExamModel.class)
public class ExamModelController {
	
    @RequestMapping(method = RequestMethod.POST, produces = "text/html")
    public String create(@Valid ExamModel examModel, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        if (bindingResult.hasErrors()) {
            populateEditForm(uiModel, examModel);
            return "exammodels/create";
        }
        uiModel.asMap().clear();
        //examModelService.saveExamModel(examModel);
        examModelService.saveModel(examModel);
        return "redirect:/exammodels/" + encodeUrlPathSegment(examModel.getId().toString(), httpServletRequest);
    }

    @RequestMapping(produces = "text/html")
    public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
        if (page != null || size != null) {
            int sizeNo = size == null ? 10 : size.intValue();
            final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
            uiModel.addAttribute("exammodels", examModelService.findExamModel(firstResult, sizeNo));
            float nrOfPages = (float) examModelService.countAllExamModels() / sizeNo;
            uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
        } else {
            uiModel.addAttribute("exammodels", examModelService.findAllExamModels());
        }
        return "exammodels/list";
    }
    
}
